package com.app.micaihu.view.main.topic.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.bean.topic.FllowTopic;
import com.app.micaihu.bean.topic.PostBean;
import com.app.micaihu.configure.Config;
import com.app.micaihu.custom.view.dataview.TopicCommentView;
import com.app.micaihu.view.main.topic.PostDetailActivity;
import com.app.micaihu.view.newsdetail.ImageShowActivity;
import com.app.utils.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 我关注的适配器
 * Created by linkai on 2016/9/9.
 */
public class FllowTopicAdapter extends AppBaseAdapter<FllowTopic> implements View.OnClickListener {

    public FllowTopicAdapter(List<FllowTopic> list, Context context) {
        super(list, context);
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        FllowTopic fllowTopic = list.get(position);
        int type = getItemViewType(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            if (type == 0) {
                //如果是空的 则显示空view
                convertView = inflater.inflate(R.layout.layout_fllowtopic_empty, null);
                return convertView;
            } else {
                viewHolder = new ViewHolder();
                convertView = inflater.inflate(R.layout.item_topic_fllow, null);
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.commentLayout = (ViewGroup) convertView.findViewById(R.id.framelayout);
                viewHolder.postCount = (TextView) convertView.findViewById(R.id.postCount);
                convertView.setTag(viewHolder);
            }

        } else {
            if (type == 0) {
                return convertView;
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
                if (viewHolder == null) {
                    viewHolder = new ViewHolder();
                    convertView = inflater.inflate(R.layout.item_topic_fllow, null);
                    viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                    viewHolder.commentLayout = (ViewGroup) convertView.findViewById(R.id.framelayout);
                    viewHolder.postCount = (TextView) convertView.findViewById(R.id.postCount);
                    convertView.setTag(viewHolder);
                }
            }
        }
        if (convertView.getVisibility() == View.GONE) {
            convertView.setVisibility(View.VISIBLE);
        }
        viewHolder.title.setText("#" + fllowTopic.getTitle() + "#");
        viewHolder.postCount.setText(fllowTopic.getCommentNum() + "帖子");
        //添加子评论
        if (fllowTopic.getCmtList() != null && fllowTopic.getCmtList().size() > 0) {
            viewHolder.commentLayout.removeAllViews();
            for (int i = 0; i < fllowTopic.getCmtList().size(); i++) {
                if (fllowTopic.getCmtList().get(i) != null) {
                    TopicCommentView view = new TopicCommentView(context, this);
                    view.setData(fllowTopic.getCmtList().get(i));
                    viewHolder.commentLayout.addView(view);
                }
            }
            viewHolder.commentLayout.setVisibility(View.VISIBLE);
        } else {
            viewHolder.commentLayout.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        FllowTopic fllowTopic = list.get(position);
        if (fllowTopic == null) {
            return 0;
        }
        return 1;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        if (v instanceof TopicCommentView) {
            //帖子点击
            PostBean postBean = ((TopicCommentView) v).getPostBean();
            Intent intent = new Intent(context, PostDetailActivity.class);
            if (postBean == null) {
                ToastUtils.makeText("此贴子已被删除");
                return;
            }
            intent.putExtra(Config.Extra.PARAMETER1, postBean.getTopicId());
            intent.putExtra(Config.Extra.PARAMETER2, postBean.getId());
            context.startActivity(intent);
        } else {
            //图片点击
            ArrayList imgList = (ArrayList) v.getTag(R.id.tag_first);
            int index = (int) v.getTag(R.id.tag_secong);
            showImageDetail(imgList, index);
        }

    }

    /**
     * 跳转图库详情
     *
     * @param imgList
     * @param i
     */
    private void showImageDetail(ArrayList imgList, int i) {
        Intent intent = new Intent(context, ImageShowActivity.class);
        intent.putStringArrayListExtra(Config.Extra.PARAMETER1, imgList);
        intent.putExtra(Config.Extra.PARAMETER2, i);
        intent.setClass(context, ImageShowActivity.class);
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.base_alpha_in, R.anim.base_slide_remain);// 前一个参数是下一个Activity的效果
    }

    private class ViewHolder {
        TextView title;
        ViewGroup commentLayout;
        TextView postCount;
    }
}
